package com.greendeliver.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.test.R;
import com.greendeliver.business.ServiceManager;

public class ServiceScheduleDetailActivity extends Activity
{

	private ArrayList<HashMap<String, String>>	servicesInfos;
	private String								ID_SERVICE;

	private TextView							textTitleService;
	private TextView							textOpening;
	private TextView							textClosing;

	private ProgressDialog						pDialog;
	private Intent								i;

	// JSON Node names
	private static final String					TAG_SUCCESS	= "success";
	private static final String					TAG_EVENTS	= "events";
	private static final String					TAG_ID		= "ID_service";
	private static final String					TAG_NAME	= "name_service";
	private static final String					TAG_OPENING	= "opening_hour_service";
	private static final String					TAG_CLOSING	= "closing_hour_service";

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_service_schedule_detail );

		servicesInfos = new ArrayList<HashMap<String, String>>();
		this.pDialog = new ProgressDialog( ServiceScheduleDetailActivity.this );

		this.textTitleService = (TextView) findViewById( R.id.textTitleService );
		this.textOpening = (TextView) findViewById( R.id.textOpeningService );
		this.textClosing = (TextView) findViewById( R.id.textClosingService );

		Intent i = getIntent();
		this.ID_SERVICE = i.getStringExtra( TAG_ID );

		new Load().execute();

		Log.d( "id2", ID_SERVICE );

	}

	class Load extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
			Log.d( "Coucou", "1" );
			pDialog.setMessage( "Chargement des Horaires" );
			pDialog.setIndeterminate( false );
			pDialog.setCancelable( true );
			pDialog.show();
			Log.d( "Coucou", "2" );
		}

		/**
		 * getting All products from url
		 * */
		@Override
		protected String doInBackground( String... args )
		{
			Log.d( "Coucou", "3" );
			ServiceManager sm = new ServiceManager();
			Log.d( "Coucou", "4" );
			Log.d( ID_SERVICE, "REGARDE" );
			servicesInfos = sm.get_schedule_service_by_ID( ID_SERVICE );
			Log.d( "Coucou", "5" );

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		@Override
		protected void onPostExecute( String file_url )
		{
			pDialog.dismiss();

			Log.d( "Coucou", "6" );

			Log.d( "Coucou", "7" );

			if ( servicesInfos.isEmpty() != true )
			{

				textTitleService.setText( servicesInfos.get( 0 ).get( TAG_NAME ) );

				textOpening.setText( servicesInfos.get( 0 ).get( TAG_OPENING ) );

				textClosing.setText( servicesInfos.get( 0 ).get( TAG_CLOSING ) );
			}

		}
	}

}
